using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;

namespace TowerDefense_Dx.Core
{
    public class DxMouse
    {
        protected Device _mice = null;
        protected byte[] _buttonPressed;
        protected Control _parent;
        private MouseState _state;

        public DxMouse(Control target)
        {
            _parent = target;
            _mice = new Device(SystemGuid.Mouse);      
            _mice.SetCooperativeLevel(_parent, CooperativeLevelFlags.Foreground
            | CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.NoWindowsKey);
            _mice.SetDataFormat(DeviceDataFormat.Mouse);
            //while (true)
            //{
            //    System.Threading.Thread.Sleep(100);
            //    Application.DoEvents();
            //    MouseState _state;
            //    _state = GetMouseState();

            //}
        }
        public MouseState GetMouseState()
        {
            do
            {
                try
                {
                    _state = _mice.CurrentMouseState;
                    break;
                }
                catch (InputException)
                {
                    Application.DoEvents();
                    try
                    {
                        _mice.Acquire();
                    }
                    catch (InputLostException)
                    {
                        continue;
                    }
                    catch (OtherApplicationHasPriorityException)
                    {
                        continue;
                    }
                }
            } while (true);
            return _state;
        }
    }
}
